const express = require('express')
const path = require('path')

const app = express()
const bodyParser = require('body-parser')

app.use(express.json());
const cors = require('cors');

app.use(bodyParser.urlencoded({
  extends: true,
  limit:'20mb'

}))


app.use(cors());
app.all('*', function(req, res, next) {
  //设为指定的域
  res.header('Access-Control-Allow-Origin', "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  res.header('Access-Control-Allow-Headers', 'Content-Type');
  res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
  res.header('Access-Control-Allow-Credentials', true);
  res.header("X-Powered-By", ' 3.2.1');
  next();
});


/**
 * 全系统允许跨域处理 这段配置要再new出express实例的时候就要设置了，放在所有的api前面，不然没有效果
*/
// app.all("*", function (req, res, next) {
//     //设置允许跨域的域名，*代表允许任意域名跨域
//     res.header("Access-Control-Allow-Origin", "*");
//     //允许的header类型
//
//   res.header("Access-Control-Allow-Headers","*");
//   // res.header('Access-Control-Allow-Origin', req.headers.origin || '*');
//     //跨域允许的请求方式
//   res.header("Access-Control-Allow-Methods","DELETE,PUT,POST,GET,OPTIONS");
//     res.header("X-Powered-By", "3.2.1");
//     res.header("Content-Type","application/json;charset=utf-8");
//
//
//
//     if (req.method.toLowerCase() == 'options')
//         res.send(200);  //让options尝试请求快速结束
//     else
//         next();
//   // res.header('Access-Control-Allow-Origin', '*');
//   // next();
// });







const expressJwt = require('express-jwt')
const userRouter = require('./router/userRouter')
const stuRouter = require('./router/stuRoyute')
const teaRouter = require('./router/teaRoute')
const roleRouter = require('./router/roleRoutr')
const menuRouter = require('./router/menuRoute')
const locationRouter = require('./router/locaRouter')
const randomRouter = require('./router/randomRouyer')
const faceRouter = require('./router/faceRouter')
const logRouter = require('./router/user-logRouter')

const history = require('connect-history-api-fallback')
const bodyParse = require("express");
// app.use(express.static(path.join(__dirname, 'public')))
// app.use('public', express.static('./public'))
app.use('/public',express.static('public'));//将文件设置成静态

//解决跨域
/*app.all('*', function (req, res, next) {
  res.header('Access-Control-Allow-Origin', '*');  //设置允许跨域的域名，*代表允许任意域名跨域
  res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With');//允许的header类型
  res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS'); //跨域允许的请求方式
  if (req.method == 'OPTIONS') {
    res.send(200);//让options尝试请求快速结束
  } else {
    next();
  }
});*/



app.get('/', (req, res) => {
  res.json({
    code: 200,
    message: 'success into api',
  })
})

app.use(history())




app.use(expressJwt({
  secret: 'express' // 签名的密钥 或 PublicKey
}).unless({
  path: [
    '/',
    '/user/login',
    '/stu/uploadimage',
    '/stu/addbyxls',
    '/tea/uploadimage',
    '/tea/addteainfobyxls',
  ] // 指定路径不经过 Token 解析
}))

app.use(function(err, req, res, next) {
  if (err.name === 'UnauthorizedError') {
    res.status(401).send('token已失效')
    // res.status(200).send('token已失效')
  } else {
    res.status(500).send('服务器错误')

  }
})

//  使用路由 / 是路由指向名称
app.use('/user', userRouter)
app.use('/user-log', logRouter)
app.use('/role', roleRouter)
app.use('/menu', menuRouter)
app.use('/face', faceRouter)
// app.use('/location', locationRouter)
// app.use('/random', randomRouter)
app.use('/stu', stuRouter)
app.use('/tea', teaRouter)


app.use(bodyParse.json()) // 指定post请求的数据为json数据


// 配置服务端口
const port = 8002
const hostname = '127.0.0.1'
app.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`)
})

